
#include "aws_esp32/aws_iot_Publish.h"
#include "mac.h"

char WIFI_MAC[MAC_Length + 1] = {0};

static const char *TAG = "mac address";
esp_err_t mac_address(void)
{
    uint8_t base_mac_addr[6] = {0};             //修改为全局变量
    extern volatile unsigned int ID_first_four; //MAC初4位做识别
    extern volatile unsigned int ID_mid_four;   //MAC中间4位做识别
    extern volatile unsigned int ID_last_four;  //MAC最后4位做识别
    esp_err_t ret = ESP_OK;
    esp_efuse_mac_get_default(base_mac_addr); //读取baic mac
    //检查是否成功读取
    if (ret != ESP_OK)
    {
        ESP_LOGE(TAG, "Failed to get base MAC address from EFUSE BLK0. (%s)", esp_err_to_name(ret));
        ESP_LOGE(TAG, "Aborting");
        abort();
    }
    else
    {
        //ESP_LOGI(TAG, "Base MAC Address read from EFUSE BLK0");
    }
    //打印读取到的mac
    //ESP_LOGI(TAG, "Using \"0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\" as base MAC address", base_mac_addr[0], base_mac_addr[1], base_mac_addr[2], base_mac_addr[3], base_mac_addr[4], base_mac_addr[5]);
    //获取mac最后四位为识别号

    ID_first_four = base_mac_addr[0];
    ID_first_four = ID_first_four << 8;
    ID_first_four += base_mac_addr[1];

    ID_mid_four = base_mac_addr[2];
    ID_mid_four = ID_mid_four << 8;
    ID_mid_four += base_mac_addr[3];

    ID_last_four = base_mac_addr[4];
    ID_last_four = ID_last_four << 8;
    ID_last_four += base_mac_addr[5];

    AWS_IoT_PublishMsg.PubMsg_ID[0] = (ID_first_four>>8)&0xff;
    AWS_IoT_PublishMsg.PubMsg_ID[1] = ID_first_four&0xff;
    AWS_IoT_PublishMsg.PubMsg_ID[2] = (ID_mid_four>>8)&0xff;
    AWS_IoT_PublishMsg.PubMsg_ID[3] = ID_mid_four&0xff;
    AWS_IoT_PublishMsg.PubMsg_ID[4] = (ID_last_four>>8)&0xff;
    AWS_IoT_PublishMsg.PubMsg_ID[5] = ID_last_four&0xff;
    sprintf(WIFI_MAC,"%04x%04x%04x",ID_first_four,ID_mid_four,ID_last_four);
    ESP_LOGI(TAG, "WIFI MAC is : %s", WIFI_MAC);
    //ESP_LOGI(TAG, "MAC mid four is : %x", ID_mid_four);
    //ESP_LOGI(TAG, "MAC last four is : %x", ID_last_four);
    return ESP_OK;
}
